#include "stdafx.h"
#include "Airway.h"

void Airway::ParseDictionary(Dictionary p_Dictionary)
{
	Dictionary::iterator p = p_Dictionary.begin();
	Name = p->second; p++;
	std::string t_Direction = p->second; p++;
	Direction = AirwayDirection_Both;
	if(t_Direction=="Previous")	
	{
		Direction = AirwayDirection_Previous;
	}
	else if(t_Direction=="Next")
	{
		Direction = AirwayDirection_Next;
	}
	Fixes.clear();
	while(p!=p_Dictionary.end())
	{
		Fixes.push_back(p->second);
		p++;
	}
}

std::string Airway::Key()
{
	return Name;
}

void Airway::Print()
{
	std::cout << "Airway name: " << Name << std::endl;
    std::cout << "Direction: " << Direction << std::endl;
	for(std::list<std::string>::iterator p=Fixes.begin(); p!=Fixes.end(); p++)
	{
		std::cout << "Fix: " << *p << std::endl;
	}
    std::cout << "------------------------------" << std::endl;
}

std::list<std::string> Airway::GetFixesByEdge(std::string p_Start, std::string p_End)
{
	std::list<std::string> t_List;
	int t_Start = 0;
	int t_End = 0;
	int i = 1;
	for(std::list<std::string>::iterator p=Fixes.begin(); p!=Fixes.end(); p++, i++)
	{
		if(*p==p_Start) t_Start = i;
		if(*p==p_End) t_End = i;
	}
	if((t_Start!=0) && (t_End!=0))
	{
		if(t_Start<t_End)
		{
			i = 1;
			for(std::list<std::string>::iterator p=Fixes.begin(); p!=Fixes.end(); p++, i++)
			{
				if((i>=t_Start) && (i<=t_End))
				{
					t_List.push_back(*p);
				}
			}
		}
		else if(t_Start>t_End)
		{
			i = 1;
			for(std::list<std::string>::iterator p=Fixes.begin(); p!=Fixes.end(); p++, i++)
			{
				if((i>=t_End) && (i<=t_Start))
				{
					t_List.push_back(*p);
				}
			}
			t_List.reverse();
		}
		else
		{
			t_List.push_back(p_Start);
		}
	}
	return t_List;
}

AirwayMap::AirwayMap() : AdaptationMap<Airway>(AIRWAYADAPTFILENAME)
{
}
